package com.fc.example.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;

import com.anythink.core.api.ATBiddingListener;
import com.anythink.nativead.api.ATNativeAdView;
import com.anythink.nativead.api.ATNativePrepareInfo;
import com.anythink.nativead.unitgroup.api.CustomNativeAd;
import com.yfanads.android.core.nat.YFAdNativeExpressAds;
import com.yfanads.android.core.nat.YFNativeExpressListener;
import com.yfanads.android.model.ExpView;
import com.yfanads.android.model.YFAdError;
import com.yfanads.android.utils.YFLog;

import java.util.List;

public class YFNativeExpressAd extends CustomNativeAd implements YFNativeExpressListener {
    private final String TAG = YFNativeExpressAd.class.getSimpleName();
    YFAdNativeExpressAds easyNativeExpress;
    Context mContext;
    ExpView mExpressView;
    FrameLayout mADLayout;
    ATBiddingListener mBiddingListener;
    Activity currentActivity;
    public YFNativeExpressAd(Context context
            , YFAdNativeExpressAds nativeBannerAd, ATBiddingListener biddingListener) {
        mContext = context;
        mBiddingListener = biddingListener;
        easyNativeExpress = nativeBannerAd;
        mADLayout = new FrameLayout(mContext);
    }

    public void loadAd(String pid, int width, int height) {
        easyNativeExpress.setAdsNumbers(1);
        easyNativeExpress.setViewAcceptedSize(width, height);
        easyNativeExpress.loadOnly(pid);
    }

    @Override
    public View getAdMediaView(Object... object) {
        if (object[0] instanceof ATNativeAdView) {
            try {
                Activity activity = (Activity) (((ATNativeAdView) object[0]).getContext());
                handleRender(activity);
            } catch (Exception e) {
                Activity activity = (Activity) mContext;
                handleRender(activity);
            }
        } else {
            Activity activity = (Activity) mContext;
            handleRender(activity);
        }
        return mADLayout;
    }

    private void handleRender(Activity activity) {
        YFLog.info(TAG + " getAdMediaView activity:" + activity);
        currentActivity = activity;
        easyNativeExpress.showAds(activity);
    }

    @Override
    public boolean isNativeExpress() {
        return true;
    }

    @Override
    public void destroy() {
        if (easyNativeExpress != null) {
            easyNativeExpress.destroy();
            easyNativeExpress = null;
        }
        currentActivity = null;
        mContext = null;
    }

    @Override
    public void onAdRenderSuccess(List<ExpView> list) {
        mExpressView = list.get(0);
        if (mExpressView.getView() != null) {
            mADLayout.addView(mExpressView.getView());
        } else {
            if (currentActivity != null) {
                FrameLayout frameLayout = new FrameLayout(currentActivity);
                mExpressView.render(frameLayout);
                mADLayout.addView(frameLayout);
            } else {
                mExpressView.render(mADLayout);
            }
        }
    }

    @Override
    public void onAdSuccess() {
    }

    @Override
    public void onAdFailed(YFAdError yfAdError) {
    }

    @Override
    public void onAdRenderFailed() {

    }

    @Override
    public void onAdExposure(ExpView expView) {
        notifyAdImpression();
    }

    @Override
    public void onAdRenderFailed(ExpView expView) {

    }

    @Override
    public void onAdClicked(ExpView expView) {
        notifyAdClicked();
    }

    @Override
    public void onAdClosed(ExpView expView) {
        try {
            if (mExpressView != null && mExpressView.getView() != null) {
                ViewParent parent = mExpressView.getView().getParent();
                if (parent instanceof ViewGroup) {
                    ((ViewGroup) parent).removeView(mExpressView.getView());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        notifyAdDislikeClick();
    }

}
